#include "buytradevaluesdialog.h"
#include "ui_buytradevaluesdialog.h"
#include <QDebug>

BuyTradeValuesDialog::BuyTradeValuesDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::BuyTradeValuesDialog)
{
    ui->setupUi(this);
}

BuyTradeValuesDialog::BuyTradeValuesDialog(Trade *trade, QWidget *parent):
    QDialog(parent),
    ui(new Ui::BuyTradeValuesDialog)
{
    ui->setupUi(this);
    this->trade=trade;
}

BuyTradeValuesDialog::~BuyTradeValuesDialog()
{
    delete ui;
}

void BuyTradeValuesDialog::setTradeValues(const Trade &trade)
{
    ui->provisionEdit->setText(trade.getValue("PROVISION"));
    ui->countEdit->setText(trade.getValue("COUNT"));
    ui->dateEdit->setDate(QDate::fromString(trade.getValue("BUY_DATE"),"dd.MM.yyyy"));
    ui->askEdit->setText(trade.getValue("ASK"));
}

void BuyTradeValuesDialog::setTradeValues()
{
    qDebug()<<"Setting up QLineText with trade values: ";
    if(trade)
    {
        qDebug()<<"PROVISION: "<<trade->getValue("PROVISION");
        ui->provisionEdit->setText(trade->getValue("PROVISION"));
        ui->countEdit->setText(trade->getValue("COUNT"));
        ui->dateEdit->setDate(QDate::fromString(trade->getValue("BUY_DATE"),"dd.MM.yyyy"));
        ui->askEdit->setText(trade->getValue("ASK"));
    }
}

void BuyTradeValuesDialog::accept()
{
    if(trade)
    {
        trade->addValue("PROVISION",ui->provisionEdit->text());
        trade->addValue("COUNT",ui->countEdit->text());
        trade->addValue("BUY_DATE",ui->dateEdit->date().toString("dd.MM.yyyy"));
        trade->addValue("ASK",ui->askEdit->text());

        emit acceptedValues(trade);
    }
}
